package securityguy;

import java.util.ArrayList;

public class Security implements Subject {

    private ArrayList watchers;
    private int code;

    public Security() {
        this.watchers = new ArrayList();
    }

    @Override
    public void registerObserver(Observer o) {
        watchers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        int index = watchers.indexOf(o);
        if (index >= 0) {
            watchers.remove(index);
        }
    }

    @Override
    public void notifyObservers() {
        for (int i = 0; i < watchers.size(); i++) {
            Observer observer = (Observer) watchers.get(i);
            observer.update(code);
        }
    }

    public void codeChanged() {
        notifyObservers();
    }

    public void setCode(int code) {
        this.code = code;
        codeChanged();
    }
}
